/**
 * @Created by pangzg pangzg2@yusys.com.cn on 2020-4-13 13:56:49.
 * @updated by
 * @description 警示对象组管理
 */
define(function (require, exports) {
	/**
	 * 页面加载完成时触发
	 * @param hashCode 路由ID
	 * @param data 传递数据对象
	 * @param cite 页面站点信息
	 */
	exports.ready = function (hashCode, data, cite) {
		yufp.lookup.reg('USE_ALERTM_MODE');
		yufp.custom.vue({
			el: cite.el,
			data: function () {
				/*定义校验规则，只能填字母数字 下划线*/
				var valiCodes = function (rule, value, callback) {
					var reg = /^[0-9a-zA-Z_]{1,}$/;
					if (value && reg.test(value)) {
						if (value.length > 64) {
							callback(new Error('长度不能大于64位'));
						} else {
							callback();
						}
					} else if (value && !reg.test(value)) {
						callback(new Error('能填字母、数字和下划线'));
					} else {
						callback();
					}
				};
				return {
					rulesone: [{
						required: true,
						message: '必填项'
					},
					{
						validator: valiCodes,
						trigger: 'blur'
					}
					],
					dataUrl: 'api/use/warnings/groups/queryList',
					dataGroupTargetUrl: 'api/use/warnings/groups/queryTargetList',
					dataTargetUrl: 'api/use/warnings/groups/queryNotInList',
					addFnButton: !yufp.session.checkCtrl('addFn'),
					updateFnButton: !yufp.session.checkCtrl('modifyFn'),
					deleteFnButton: !yufp.session.checkCtrl('deleteFn'),
					groupTargetFnButton: !yufp.session.checkCtrl('groupTargetFn'),
					formdata: {},
					dialogVisible: false,
					formDisabled: false,
					saveDisabled: false,
					cancelBtnShow: true,
					targetDialogVisible: false,
					addTargerDialogVisible: false,
					viewTitle: "",
					placeholderGroup: '',
					addFlag: true,
					groupDisabled: true,
					searchFormdata: {},
					addSearchFormdata: {}
				};
			},
			methods: {
				/**
				 * 单击选择
				 */
				cellClick: function (row, event, column, index) {
					var _this = this;
					if (row) {
						_this.$refs.refTable.toggleRowSelection(row);
					}
				},
				/**
				 * 单击选择dialog
				 */
				cellClickTargetTable: function (row, event, column, index) {
					var _this = this;
					if (row) {
						_this.$refs.refTargetTable.toggleRowSelection(row);
					}
				},
				/**
				 * 单击选择dialog
				 */
				cellClickAddTargetTable: function (row, event, column, index) {
					var _this = this;
					if (row) {
						_this.$refs.refAddTargetTable.toggleRowSelection(row);
					}
				},
				/**
				 * 取消新增警示组页面
				 */
				cancelFn: function () {
					var _this = this;
					_this.dialogVisible = false;
				},
				/**
				 * 取消新增警示对象页面
				 */
				cancelAddTargetFn: function () {
					var _this = this;
					_this.addTargerDialogVisible = false;
					_this.$refs.refAddTargetTable.clearSelection();
				},
				/**
				 * 保存
				 */
				saveFn: function () {
					var _this = this;
					var model = {};
					yufp.clone(_this.formdata, model);
					var validate = false;
					var url = "";
					if (_this.addFlag) {
						url = "api/use/warnings/groups/save";
					} else {
						url = "api/use/warnings/groups/update";
					}
					_this.$refs.refForm.validate(function (valid) {
						validate = valid;
					});
					if (!validate) {
						return;
					}
					// 向后台发送保存请求
					yufp.service.request({
						method: 'POST',
						url: url,
						data: model,
						callback: function (code, message, response) {
							if (response.code == 0) {
								_this.$refs.refTable.remoteData();
								_this.$message('操作成功');
								_this.dialogVisible = false;
							} else {
								_this.$message({
									message: response.message,
									type: 'error', showClose: true, duration: 0
								});
							}
						}
					});
				},
				/**
				 * 控制保存按钮、xdialog、表单的状态
				 * @param viewType 表单类型
				 * @param editable 可编辑,默认false
				 */
				switchStatus: function (viewType, editable) {
					var _this = this;
					_this.viewType = viewType;
					_this.saveBtnShow = editable;
					_this.dialogVisible = true;
					_this.formDisabled = !editable;
					_this.saveDisabled = !editable;
				},
				/**
				 * 新增按钮
				 */
				addFn: function () {
					var _this = this;
					_this.addFlag = true;
					_this.viewTitle = "预警对象组新增";
					_this.switchStatus('ADD', true);
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
					});
				},
				/**
				 * 修改
				 */
				modifyFn: function () {
					var _this = this;
					_this.addFlag = false;
					_this.viewTitle = "预警对象组修改";
					if (_this.$refs.refTable.selections.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning', duration: 6000
						});
						return;
					}
					_this.switchStatus('UPDATE', true);
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						var obj = _this.$refs.refTable.selections[0];
						yufp.clone(obj, _this.formdata);
					});
				},
				/**
				 * 详情
				 */
				infoFn: function () {
					var _this = this;
					_this.addFlag = false;
					_this.viewTitle = "预警对象组查看";
					var selectionsAry = _this.$refs.refTable.selections;
					if (selectionsAry.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning', duration: 6000
						});
						return;
					}
					_this.switchStatus('DETAIL', false);
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						yufp.clone(selectionsAry[0], _this.formdata);
					});
				},
				/**
				 * 删除
				 */
				deleteFn: function () {
					var _this = this;
					var selections = _this.$refs.refTable.selections;
					if (selections.length < 1) {
						_this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
						return;
					}
					var len = selections.length,
						arr = [];
					for (var i = 0; i < len; i++) {
						arr.push({
							groupCode: selections[i].groupCode,
							groupName: selections[i].groupName
						});
					}
					_this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning', duration: 6000,
						center: true,
						callback: function (action) {
							if (action === 'confirm') {
								yufp.service.request({
									method: 'DELETE',
									url: 'api/use/warnings/groups/delete',
									data: JSON.stringify(arr),
									callback: function (code, message, response) {
										if (response.code == '0') {
											_this.$refs.refTable.remoteData();
											_this.$message('操作成功');
										} else {
											_this.$message({ message: response.message, type: 'error', showClose: true, duration: 0 });
										}
									}
								});
							}
						}
					});
				},
				/**
				 * 警示对象组对象查看
				 */
				groupTargetFn: function () {
					var _this = this;
					var selectionsAry = _this.$refs.refTable.selections;
					if (selectionsAry.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning', duration: 6000
						});
						return;
					}
					_this.targetDialogVisible = true;
					this.$nextTick(function () {
						_this.searchFormdata.groupCode = selectionsAry[0].groupCode;
						var param = {
							condition: JSON.stringify(_this.searchFormdata)
						};
						_this.$refs.refTargetTable.remoteData(param);
					});
				},
				/**
				 * 警示对象组新增对象
				 */
				addTargetFn: function () {
					_this = this;
					_this.addTargerDialogVisible = true;
					this.$nextTick(function () {
						_this.addSearchFormdata.groupCode = _this.$refs.refTable.selections[0].groupCode;
						var param = {
							condition: JSON.stringify(_this.addSearchFormdata)
						};
						_this.$refs.refAddTargetTable.remoteData(param);
					});
				},
				/**
				 * 警示对象组删除对象
				 */
				deleteTargetFn: function () {
					var _this = this;
					var selections = _this.$refs.refTargetTable.selections;
					if (selections.length < 1) {
						_this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
						return;
					}
					var len = selections.length,
						arr = [];
					for (var i = 0; i < len; i++) {
						arr.push({
							targetCode: selections[i].targetCode,
							targetName: selections[i].targetName,
							groupCode: _this.$refs.refTable.selections[0].groupCode,
							alarmMode: selections[i].alarmMode
						});
					}
					_this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning', duration: 6000,
						center: true,
						callback: function (action) {
							if (action === 'confirm') {
								yufp.service.request({
									method: 'DELETE',
									url: 'api/use/warnings/groups/deleteGroupTarget',
									data: JSON.stringify(arr),
									callback: function (code, message, response) {
										if (response.code == 0) {
											_this.$refs.refTargetTable.remoteData();
											_this.$message('操作成功');
										} else {
											_this.$message({ message: response.message, type: 'error', showClose: true, duration: 0 });
										}
									}
								});
							}
						}
					});
				},
				/**
				 * 新增对象组对象保存
				 */
				saveGroupMemberFn: function () {
					var _this = this;
					var selections = _this.$refs.refAddTargetTable.selections;
					if (selections.length < 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning', duration: 6000
						});
						return;
					}
					var len = selections.length,
						arr = [];
					for (var i = 0; i < len; i++) {
						arr.push({
							targetCode: selections[i].targetCode,
							groupCode: _this.$refs.refTable.selections[0].groupCode,
							alarmMode: selections[i].alarmMode
						});
					}
					// 向后台发送保存请求
					yufp.service.request({
						method: 'POST',
						url: 'api/use/warnings/groups/addAlarmMember',
						data: JSON.stringify(arr),
						callback: function (code, message, response) {
							if (response.code == 0) {
								_this.$refs.refAddTargetTable.clearSelection();
								_this.$refs.refTargetTable.remoteData();
								_this.$message('操作成功');
								_this.addTargerDialogVisible = false;
							} else {
								_this.$message({ message: response.message, type: 'error', showClose: true, duration: 0 });
							}
						}
					});
				},
				/**
				 * 翻页保存数据主键
				 */
				getKey: function (row) {
					return row.targetCode + row.alarmMode;
				}
			}
		});
	};
});

